package com.chonor.mall.service.impl;

import cn.hutool.core.date.DateUtil;
import com.chonor.mall.dao.OrderReturnApplyDao;
import com.chonor.mall.dto.OrderReturnApplyResult;
import com.chonor.mall.dto.ReturnApplyQueryParam;
import com.chonor.mall.dto.UpdateStatusParam;
import com.chonor.mall.mapper.*;
import com.chonor.mall.model.*;
import com.chonor.mall.service.MemberService;
import com.chonor.mall.service.OrderReturnApplyService;
import com.github.pagehelper.PageHelper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderReturnApplyServiceImpl implements OrderReturnApplyService {

    @Autowired
    private OrderReturnApplyDao orderReturnApplyDao;

    @Autowired
    private OrderReturnApplyMapper orderReturnApplyMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<OrderReturnApply> list(ReturnApplyQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<OrderReturnApply> orderReturnApplies = orderReturnApplyDao.getList(queryParam);
//        for (OrderReturnApply item : orderReturnApplies) {
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            item.setCreateTime(formatter.);
//        }
        return orderReturnApplies;
    }

    @Override
    public int delete(List<Long> ids) {
        OrderReturnApplyExample example = new OrderReturnApplyExample();
        example.createCriteria().andIdIn(ids).andStatusEqualTo(3);
        return orderReturnApplyMapper.deleteByExample(example);
    }

    @Override
    public int updateStatus(Long id, UpdateStatusParam statusParam) {
        Integer status = statusParam.getStatus();
        OrderReturnApply returnApply = new OrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else{
            return 0;
        }
        return orderReturnApplyMapper.updateByPrimaryKeySelective(returnApply);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        //通过则，回补库存，返回积分，修改订单状态
        if(status == 1){
            OrderReturnApply orderReturnApply = getItem(id);

            //修改订单状态为已关闭
            Order order = new Order();
            order.setId(orderReturnApply.getOrderId());
            order.setStatus(4);
            order.setModifyat(new Date());
            orderMapper.updateByPrimaryKeySelective(order);
            //取消释放库存
            OrderItemExample example = new OrderItemExample();
            example.createCriteria().andOrderIdEqualTo(orderReturnApply.getOrderId());
            List<OrderItem> orderItemList = orderItemMapper.selectByExample(example);
            for (OrderItem item : orderItemList) {
                Product product = productMapper.selectByPrimaryKey(item.getProductId());
                Product update = new Product();
                update.setId(product.getId());
                update.setStock(product.getStock() + item.getProductQuantity());
                productMapper.updateByPrimaryKeySelective(update);
            }
            //返还积分
            Order update = orderMapper.selectByPrimaryKey(orderReturnApply.getOrderId());
            Member member = memberMapper.selectByPrimaryKey(update.getMemberId());
            Integer integration = member.getIntegration() + update.getUseIntegration();
            Member memberUpdate = new Member();
            memberUpdate.setId(member.getId());
            memberUpdate.setIntegration(integration);
            memberMapper.updateByPrimaryKeySelective(memberUpdate);

        }

        //修改处理状态
        OrderReturnApply update = new OrderReturnApply();
        update.setId(id);
        update.setStatus(status);
        update.setHandleTime(new Date());
        int count = orderReturnApplyMapper.updateByPrimaryKeySelective(update);

        return count;
    }

    @Override
    public OrderReturnApply getItem(Long id) {
        return orderReturnApplyMapper.selectByPrimaryKey(id);
    }
}
